﻿using BP.Difference;
using BP.Sys;
using BP.Web;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BP.DA
{
    public class SQLPort
    {
        public SQLPort()
        {
        }

        public static string Gener_Port(string mark, Hashtable ht = null)
        {
            string key = null;
            if (ht != null && ht.ContainsKey("Key") == true)
                key = ht["Key"].ToString();

            //部门树懒加载。
            if (mark.Equals("Port_DeptLazily") == true)
            {
                return "SELECT No,Name,ParentNo FROM Port_Dept WHERE ParentNo='" + key + "' OR No='" + key + "' Order By Idx ";
            }
            //根据部门获得人员
            if (mark.Equals("Port_EmpsByDeptNo") == true)
            {
                if (SystemConfig.CCBPMRunModel != CCBPMRunModel.SAAS)
                    return " SELECT B.No  AS No,B.Name AS Name  FROM port_deptemp a, Port_Emp B  where A.FK_Emp=B.No and A.FK_Dept ='" + key + "' Order By B.Idx ";
                else
                    return "SELECT UserID as No,Name FROM Port_Emp WHERE FK_Dept='" + key + "' Order By Idx ";
            }
            //搜索人员账号
            if (mark.Equals("Port_EmpSearchKey") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.SAAS)
                    return "SELECT UserID as No,Name FROM Port_Emp WHERE Name LIKE '%" + key + "%' AND OrgNo='" + WebUser.OrgNo + "' Order By Idx ";
                else
                    return " SELECT B.No,B.Name FROM port_deptemp a, Port_Emp B WHERE A.FK_Emp=B.No AND Name LIKE '%" + key + "%' Order By B.Idx  ";

            }
            //部门
            if (mark.Equals("Port_Depts") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name,ParentNo FROM Port_Dept Order by Idx ";
                else
                    return "SELECT No,Name,ParentNo FROM Port_Dept WHERE OrgNo='" + WebUser.OrgNo + "' Order by Idx ";
            }
            //人员信息
            if (mark.Equals("Port_Emps") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name,FK_Dept GroupNo FROM Port_Emp Order By Idx ";
                else
                    return "SELECT No,Name,FK_Dept GroupNo FROM Port_Emp WHERE OrgNo='" + WebUser.OrgNo + "' Order by Idx ";
            }
            //岗位类型
            if (mark.Equals("Port_StationTypes"))
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No, Name FROM Port_StationType Order by Idx ";
                else
                    return "SELECT No,Name FROM Port_StationType WHERE OrgNo = '" + WebUser.OrgNo + "' Order by Idx ";
            }
            //岗位
            if (mark.Equals("Port_Stations"))
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name,FK_StationType AS GroupNo FROM Port_Station Order By Idx ";
                else
                    return "SELECT No,Name,FK_StationType AS GroupNo FROM Port_Station WHERE OrgNo= '" + WebUser.OrgNo + "' Order By Idx ";
            }
            //组织列表
            if (mark.Equals("Port_Orgs"))
            {
                return "SELECT No,Name FROM Port_Org WHERE No != '" + WebUser.OrgNo + "'";
            }
            //人员信息 GenerListEn  beforeInsert
            if (mark.Equals("Port_EmpByDept"))
            {
                return "SELECT No,Name,Tel,Email FROM Port_Emp WHERE FK_Dept='"+WebUser.DeptNo+"'";
            }
            //组织列表   流程复制 选择租户
            if (mark.Equals("Port_SelectOrgs"))
            {
                return "SELECT No,Name FROM port_org WHERE ParentNo = 100";
            }
            //设置二级管理员 GPN_Adminer 选择管理员
            if (mark.Equals("Port_SelectAdminer"))
            {
                return "SELECT No,Name,ParentNo FROM Port_Dept WHERE ParentNo='" + key + "' OR No='" + key + "' UNION  SELECT No,Name,ParentNo FROM Port_Dept WHERE ParentNo IN ( SELECT No FROM Port_Dept WHERE ParentNo='" + key + "' OR No='" + key +"'";
            }
            //设置二级管理员 GPN_Adminer 选择管理员
            if (mark.Equals("Port_AdminerEmpByDept"))
            {
                return "SELECT No,Name FROM Port_Emp B WHERE FK_Dept='" + key + "'";
            }

            //查看人员 GL_StationRole 
            if (mark.Equals("Port_StationRole"))
            {
                return "SELECT c.Name AS DeptName, b.Name FROM port_deptempstation a , port_emp b, port_dept c WHERE a.FK_Emp = b.No AND a.FK_Dept=c.No  AND a.FK_Station = '" + key + "' ORDER BY b.Name";
            }
            //查看人员 GL_StationRole 
            if (mark.Equals("Port_TreeDept"))
            {
                return "SELECT No,Name,ParentNo FROM Port_Dept Order By ParentNo, Idx";
            }
            //Org.cs beforeUpdateInsertAction方法使用
            if (mark.Equals("Port_Users") == true)
            {
                return "SELECT COUNT(*) AS a FROM Port_Emp WHERE OrgNo='" + WebUser.OrgNo + "'";
            }
            //Org.cs beforeUpdateInsertAction方法使用
            if (mark.Equals("Port_Depts") == true)
            {
                return "SELECT COUNT(*) AS a FROM Port_Dept WHERE OrgNo='" + WebUser.OrgNo + "'";
            }
            //HuiQian 会签 增加会签人
            if (mark.Equals("Port_HuiQianEmp") == true)
            {
                return "SELECT A." + BP.Sys.Base.Glo.UserNo + ", A.Name, a.FK_Dept FROM Port_Emp A, Port_DeptEmpStation B WHERE a.No = B.FK_Emp AND B.FK_Station = '" + key + "'";
            }
            return null;
        }
    }
}
